package com.wie.staffhubpro.controller;


import com.wie.staffhubpro.domain.dto.DeptDTO;
import com.wie.staffhubpro.domain.entity.Dept;
import com.wie.staffhubpro.domain.entity.Result;
import com.wie.staffhubpro.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/dept")
public class DeptController {
    
    @Autowired
    private DeptService deptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public Result getList() {
        log.info("获取部门列表");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 新增部门
     */
    @PostMapping("/add")
    public Result add(@RequestBody DeptDTO deptDTO) {
        log.info("新增部门: {}", deptDTO);
        deptService.addDept(deptDTO);
        return Result.success();
    }

    /**
     * 根据id修改部门信息
     */
    @PostMapping("/alter")
    public Result alter(@RequestBody Dept dept) {
        log.info("修改部门信息: {}", dept);
        deptService.alterDept(dept);
        return Result.success();
    }

    /**
     * 根据id删除部门
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        Dept dept = deptService.getById(id);
        log.info("删除部门: {}", dept);
        deptService.removeById(id);
        return Result.success();
    }

    /**
     * 根据部门名称模糊查询部门
     */
    @GetMapping("/getByName")
    public Result getByName(@RequestParam String deptName) {
        log.info("模糊查询部门: {}", deptName);
        List<Dept> deptList = deptService.getByName(deptName);
        return Result.success(deptList);
    }

}
